<?php 
class unitsDAL 
{
	public function Insert($db, $unitsObj, &$errorMsg) 
	{
		$errorMsg = "";

		$query = "INSERT INTO units (name, description, deleteflag) VALUES (".ToSQLPostVAL($unitsObj->name).", ".ToSQLPostVAL($unitsObj->description).", ".ToSQLPostVAL($unitsObj->deleteflag).")";
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		$unitsObj->id = mysql_insert_id();
		return true;
	}

	public function Update($db, $unitsObj, &$errorMsg)
	 {	
		$errorMsg = "";

		$query = "UPDATE units SET name=".ToSQLPostVAL($unitsObj->name).", description=".ToSQLPostVAL($unitsObj->description).", deleteflag=".ToSQLPostVAL($unitsObj->deleteflag)." WHERE id=".ToSQLPostVAL($unitsObj->id);
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg, $joinTables="", $onFields="", $getFields="")
	{
		$errorMsg = "";
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$query	= "SELECT units.*$extraFields FROM units $joinClause WHERE units.id=".ToSQLPostVAL($id);

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
			return null;
		} else {
			if ($db->next_record()) {
				$unitsObj = new unitsObj($db->f("id"), $db->f("name"), $db->f("description"), $db->f("deleteflag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$unitsObj->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				return $unitsObj;
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}
		return null;
	}

	public function IsExist($db, $whereStr) 
	{
		$query	= "SELECT * FROM units";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

		if (!$db->query($query)) {
			return false;
		} else {
			while ($db->next_record()) {
				return true;
			}
		}
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) 
	{
		$query	= "DELETE FROM units";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else {
			$errorMsg = "You have no delete condition in query string ($query WHERE ?).";
			return false;
		}

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}
		return true;
	}

	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg, $joinTables="", $onFields="", $getFields="") 
	{
		$returnData = array();
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$errorMsg = "";
		$query	= "SELECT units.*$extraFields FROM units$joinClause";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
		} else {
			while ($db->next_record()) 	{
				$objVal = new unitsObj($db->f("id"), $db->f("name"), $db->f("description"), $db->f("deleteflag"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$objVal->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				$returnData[] = $objVal;
			}
		}
		return $returnData;
	}

	public function Push($unitsObj)
	{
		global $id, $name, $description, $deleteflag;

		$id = $unitsObj->id;
		$name = $unitsObj->name;
		$description = $unitsObj->description;
		$deleteflag = $unitsObj->deleteflag;
	}

	public function Pop(&$unitsObj)
	{
		global $id, $name, $description, $deleteflag;

		if (isset($id)) $unitsObj->id = $id;
		if (isset($name)) $unitsObj->name = $name;
		if (isset($description)) $unitsObj->description = $description;
		if (isset($deleteflag)) $unitsObj->deleteflag = $deleteflag;
	}

} 
?>